package control {
  
  import flash.display.Loader;
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.net.URLRequest;
  import flash.text.TextField;
  import flash.filters.GlowFilter;
  
  /** Base class for any rss item dispaly clip
    */ 
  public class RSSItem extends MovieClip {
    /**  **/
    public var tfTitle: TextField
    /**  **/
    public var tfDescription: TextField
    /**  **/
    public var tfDate: TextField

    public var title: String = ""
    public var description: String = ""
    public var image: String = ""
    public var link: String = ""
    
    private var imageLoader: Loader;


	/**
	 * Constructor
	 */
    public function RSSItem () {
      addEventListener(Event.ADDED_TO_STAGE, init)
    }

    private function init (event:Event=null): void {
      if (tfTitle) {
        if (title.length > 80) {
          var ind: int = title.substr(0, 80).lastIndexOf(" ")
          tfTitle.text =  title.substring(0, ind) + "..."
        } else {
          tfTitle.text = title
        }
      }

      if (tfDescription) tfDescription.text = description
      var now: Date = new Date
      if (tfDate) tfDate.text = month(now.month) + ", " + String(now.date)
      if (image && image != "") {
        tfTitle.x = 160
        tfTitle.width = 200
        
        trace("NewsClip: " + image)
        
        imageLoader = new Loader()
        imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImage)
        imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
        imageLoader.load(new URLRequest(image))
        imageLoader.x = 120
        addChildAt(imageLoader, 0)
      }

      if (link) {
        graphics.beginFill(0xFF0000, 0.0)
        graphics.drawRect(0, 0, 468, 60)
        graphics.endFill()
      }

      var over: MovieClip = new MovieClip
      over.graphics.beginFill(0xFF0000, 0.0)
      over.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight)
      over.graphics.endFill()
      over.useHandCursor = true
      over.buttonMode = true
      addChild(over)
    }

    public function set textColor (value:uint): void {
      tfTitle.textColor = value
    }

    private function onLoadImage (event:Event): void {
      imageLoader.filters = [new GlowFilter(0xFFFFFF, 1.0, 10, 10, 2, 3)]
      var hs: Number = stage.stageHeight / imageLoader.height
      var ws: Number = stage.stageWidth  / imageLoader.width
      var a: Number = hs > ws ? ws : hs
      imageLoader.scaleX = imageLoader.scaleY = a
    }

	/**
	 * Input/output error handler
	 * 
	 * @param event
	 */
    private function onIOError (event:IOErrorEvent): void {
      trace("ERR: NewsClip.onIOError: " + event.text)
      tfTitle.x = 100
      tfTitle.width = 260
    }

	/**
	 * Translate month from number to short word
	 * 
	 * @param ind
	 */
    private function month (ind:int): String {
      switch (ind) {
        case 0: return "���"
        case 1: return "���"
        case 2: return "����"
        case 3: return "���"
        case 4: return "���"
        case 5: return "����"
        case 6: return "����"
        case 7: return "���"
        case 8: return "���"
        case 9: return "���"
        case 10: return "���"
        case 11: return "���"
      }
      return ""
    }
  }
}